/* global window */
import modelExtend from 'dva-model-extend'
import * as returnOrderService from './services/returnOrder'
import { pageModel } from 'utils/model'

const { query } = returnOrderService

export default modelExtend(pageModel, {
  namespace: 'returnOrder',

  state: {
    currentItem: {},
    expandForm: false,
    modalVisible: false,
    modalType: 'create',
    selectedRowKeys: [],
    selectRecord: {},
    disable: false,
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen((location) => {
        if (location.pathname === '/returnOrder') {
          const payload = location.query || { current: 1, size: 10 }
          dispatch({
            type: 'query',
            payload,
          })
        }
      })
    },
  },

  effects: {

    * query({ payload = {} }, { call, put }) {
      const response = yield call(query, payload)
      const { data } = response
      if (response && data) {
        yield put({
          type: 'querySuccess',
          payload: {
            list: data.page.records,
            pagination: {
              current: Number(payload.current) || 1,
              pageSize: Number(payload.size) || 10,
              total: data.page.total,
            },
          },
        })
      }
    },

    * delete({ payload }, { call, put, select }) {
      const data = yield call(returnOrderService.remove, { id: payload })
      const { selectedRowKeys } = yield select(_ => _.returnOrder)
      if (data.code === 0) {
        yield put({ type: 'updateState', payload: { selectedRowKeys: selectedRowKeys.filter(_ => _ !== payload) } })
      } else {
        throw data
      }
    },



    * multiDelete({ payload }, { call, put }) {
      const data = yield call(returnOrderService.batchDelete, payload)
      if (data.code === 0) {
        yield put({ type: 'updateState', payload: { selectedRowKeys: [] } })
      } else {
        throw data
      }
    },


    * create({ payload }, { call, put }) {
      const data = yield call(returnOrderService.create, payload)
      if (data.code === 0) {
        yield put({ type: 'hideModal' })
      } else {
        throw data
      }
    },

    * update({ payload }, { select, call, put }) {
      const id = yield select(({ returnOrder }) => {
        if (payload.id) {
          return payload.id
        }
        return returnOrder.currentItem.id
      })

      const newObj = { ...payload, id }
      const data = yield call(returnOrderService.update, newObj)
      if (data.code === 0) {
        yield put({
          type: 'hideModal', payload: {
            selectedRowKeys: [],
            selectRecord: {},
            resetRoleModalVisible: false,
          }
        })
      } else {
        throw data
      }
    },

  },

  reducers: {

    showModal(state, { payload }) {
      return { ...state, ...payload, modalVisible: true }
    },


    hideModal(state) {
      return { ...state, modalVisible: false }
    },

    toggleForm(state) {
      const { expandForm } = state
      return {
        ...state,
        expandForm: !expandForm,
      }
    },

  },
})
